//
//  StatusOriginalView.swift
//  Weobo
//
//  Created by 史亚洲 on 16/1/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class StatusOriginalView: UIView {
    
    //定义对象属性来记录  Constraint就是SnapKit中约束对象的类型
    var bottomConstant: Constraint?
    
    @objc private func gotoSource() {
        //跳转到web页面
        let web=WebViewController()
        web.urlString=status?.source?.weiboSource().link
        navController()?.pushViewController(web, animated: true)
        
        
        
    }
    
    var status: Status? {
    
        didSet {
            //绑定数据
            headImageView.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text=status?.user?.name
            mbrankImage.image=status?.user?.mbrank_image
            verifiedTypeImage.image=status?.user?.verified_type_image
            timeLabel.text=status?.created_at?.sinaDateString()
            //sourceLabel.text=status?.source
            
            sourceButton.setTitle(status?.source?.weiboSource().source, forState: .Normal)
            
            
            
            contentLabel.text=status?.text
            //传递 配图视图的数据源
            pictureView.imageURLs=status?.imageURLs
            
            /*
            注意:在更新约束之前,将之前的约束卸载掉
            需要根据配图视图的数量,动态调整约束,确保底部只有一条约束存在
            
            */
            
            self.bottomConstant?.uninstall()
            
            
            //需要根据配图的数量,动态调整约束
            if let urls = status?.imageURLs {
                print(urls.count)
            
            //如果urls不为空,修改底部约束,将之前的约束重写修改回来
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstant=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
            
            
            } else {
            //urls为空,修改约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstant=make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            }

        }
    
    
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //MARK:2.设置约束,设置UI
    private func setupUI() {
        //backgroundColor=UIColor(white: 0.95, alpha: 1.0)
        //设置分割条的颜色
        sepView.backgroundColor=UIColor.redColor()
        
        addSubview(sepView)
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(timeLabel)
        addSubview(sourceButton)
        addSubview(verifiedTypeImage)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        
        //1.头像
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: StatusCellImageWidth, height: StatusCellImageWidth))
        }
        
        //2.名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_top)
            make.left.equalTo(headImageView.snp_right).offset(StatusCellMargin)
        }
        
        //3.会员等级
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        //4.时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_bottom)
            make.left.equalTo(headImageView.snp_right).offset(StatusCellMargin)
        }
    
        //5.资源
        sourceButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo (timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        //6.微博认证类型
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        //7.微博正文
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImageView.snp_left)
            make.top.equalTo(headImageView.snp_bottom).offset(StatusCellMargin)
        }
        
        //8.配图视图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImageView.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            //后续修改,先测试大小
            //make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
    
        
    //设置一个非常关键的属性,就可以实现视图的行高自动获取
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstant=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        //添加点击事件
        sourceButton.addTarget(self, action: "gotoSource", forControlEvents: .TouchUpInside)
    
    
    }
    
    
    //MARK:1.懒加载所有的子视图
    //分隔条
    private lazy var sepView: UIView = UIView()
    //1.用户头像
    private lazy var headImageView: UIImageView = UIImageView()
    
    //2.用户名
    private lazy var nameLabel: UILabel = UILabel(title: "十万个冷笑话", fontSize: 14, color: themeColor)
    
    //3.会员等级
    private lazy var mbrankImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    
    //4.时间
    lazy var timeLabel: UILabel = UILabel(title: "12:00", fontSize: 10, color: UIColor.lightGrayColor())
    
    //5.来源
   // private lazy var sourceLabel:UILabel = UILabel(title: "22期的同学们", fontSize: 10, color: UIColor.lightGrayColor())
    private lazy var sourceButton: UIButton = UIButton(title: "",color: UIColor.lightGrayColor(), fontSize: 10,backImageName: nil)
    
    //6.微博认证类型
    private lazy var verifiedTypeImage: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //7.微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "只有想不到,没有看不到", fontSize: 14, color: UIColor.darkGrayColor(),margin:  StatusCellMargin)
    
    //8.配图视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
    
    

}
